package common.dao.Impl;

import cn.hutool.core.util.StrUtil;
import common.dao.UserDao;
import common.domain.User;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(User user) {
        String sql = "insert into user\n" +
                "        (username,password,sex,phone,email) \n" +
                "values(?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,user.getUsername());
            psmt.setString(2,user.getPassword());
            psmt.setString(3,user.getSex());
            psmt.setString(4,user.getPhone());
            psmt.setString(5,user.getEmail());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(User user) {
        String sql = "update user set";
        List<Object> param = new ArrayList<>();
        if (user == null){
            return;
        }else{
            if(user.getUserId() != null){
                sql += " user_id = ?,";
                param.add(user.getUserId());
            }
            if(user.getUsername() != null){
                sql += " username = ?,";
                param.add(user.getUsername());
            }
            if(user.getPassword() != null){
                sql += " password = ?,";
                param.add(user.getPassword());
            }
            if(user.getSex() != null){
                sql += " sex = ?,";
                param.add(user.getSex());
            }
            if(user.getPhone() != null){
                sql += " phone = ?,";
                param.add(user.getPhone());
            }
            if(user.getEmail() != null){
                sql += " email = ?,";
                param.add(user.getEmail());
            }
            if(user.getCreateTime() != null){
                sql += " create_time = ?,";
                param.add(user.getCreateTime());
            }
            if(user.getUpdateTime() != null){
                sql += " update_time = ?,";
                param.add(user.getUpdateTime());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where user_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,user.getUserId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from user where user_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public User selectById(Long id) {
        String sql = "select * from user where user_id = ?";
        User user = new User();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                user.setUserId(rs.getLong(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setSex(rs.getString(4));
                user.setPhone(rs.getString(5));
                user.setEmail(rs.getString(6));
                user.setCreateTime(rs.getDate(7));
                user.setUpdateTime(rs.getDate(8));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return user;
    }

    @Override
    public List<User> selectList(User user) {
        String sql = "select * from user where 1=1";
        List<User> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (user == null){
            sql = "select * from user";
        }else{
            if(user.getUserId() != null){
                sql += " and user_id = ?";
                param.add(user.getUserId());
            }
            if(user.getUsername() != null){
                sql += " and username like \"%\"?\"%\"";
                param.add(user.getUsername());
            }
            if(user.getPassword() != null){
                sql += " and password like \"%\"?\"%\"";
                param.add(user.getPassword());
            }
            if(user.getSex() != null){
                sql += " and sex = ?";
                param.add(user.getSex());
            }
            if(user.getPhone() != null){
                sql += " and phone like \"%\"?\"%\"";
                param.add(user.getPhone());
            }
            if(user.getEmail() != null){
                sql += " and email like \"%\"?\"%\"";
                param.add(user.getEmail());
            }
            if(user.getCreateTime() != null){
                sql += " and create_time = ?";
                param.add(user.getCreateTime());
            }
            if(user.getUpdateTime() != null){
                sql += " and update_time = ?";
                param.add(user.getUpdateTime());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                User user1 = new User();
                user1.setUserId(rs.getLong(1));
                user1.setUsername(rs.getString(2));
                user1.setPassword(rs.getString(3));
                user1.setSex(rs.getString(4));
                user1.setPhone(rs.getString(5));
                user1.setEmail(rs.getString(6));
                user1.setCreateTime(rs.getDate(7));
                user1.setUpdateTime(rs.getDate(8));
                list.add(user1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
